//获取应用实例
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yes: true,
    no: false,
    content: '',
    detail_img: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  // 说说内容
  titleInput: function(e) {
    this.setData({
      content: e.detail.value
    });
  },

  //添加商品图片
  editGoodsImgEvent: function() {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed '], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        var tempFilePaths = res.tempFilePaths;
        util.showLoading("加载中...");
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: app.globalData.base_url + 'img/saveImage',
            filePath: tempFilePaths[i],
            name: 'file',
            success: function(res) {
              util.hiddenLoading();
              var data = JSON.parse(res.data);
              that.data.detail_img.push(data.data.url);
              that.setData({
                detail_img: that.data.detail_img
              })
            },
            fail: function() {
              util.hiddenLoading();
            }
          })
        }
      }
    })
  },
  deleteImg: function(e) {
    var index = e.currentTarget.dataset.index;
    var data = this.data.detail_img;
    data.splice(index, 1);
    this.setData({
      detail_img: data
    })
  },
  itemImgClick: function(e) {
    var url = e.currentTarget.dataset.url;
    var that = this;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: that.data.detail_img // 需要预览的图片http链接列表
    })
  },

  submit: function() {
    var that = this;
    var content = that.data.content;
    content = content.replace(/(^\s*)|(\s*$)/g, "");   //去除前后空格
    if (content.length <= 0) {
      util.showError('说说内容不能为空')
      return;
    }
    var params = {
      content: that.data.content,
      images: that.data.detail_img
    }
    // params = JSON.stringify(params);
    util.showLoading("提交中...");
    api.POST({
      url: app.globalData.base_url + 'topic/publish',
      params: params,
      success: function(res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          util.showStatus("提交成功");
          wx.navigateBack({
              delta:1
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function(res) {
        wx.hideLoading();
        util.showError("请检查网络");
      }
    });
  },

})